<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php 
	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Letter To Clinic </title>
<link href="css/template.css" rel="stylesheet" type="text/css" />
<script src="js/droplinemenu.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
//build menu with DIV ID="myslidemenu" on page:
droplinemenu.buildmenu("nav")
</script>

<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>


<script>
	


	


	


	




	
	function printLetter(){
		document.getElementById("header").style.display = "none";
		document.getElementById("headerTitle").style.display = "none";
		document.getElementById("nav").style.display = "none";
		document.getElementById("operateDiv").style.display = "none";
		document.getElementById("list").style.display = "none";
		document.getElementById("footer").style.display = "none";
		document.getElementById("letterHeaderPrint").style.display = "block";
		document.getElementById("letterHeader").style.display = "none";
		replaceLetterHeader();
		window.print();
		document.getElementById("letterHeaderPrint").style.display = "none";
		document.getElementById("letterHeader").style.display = "block";
		document.getElementById("header").style.display = "block";
		document.getElementById("headerTitle").style.display = "block";
		document.getElementById("nav").style.display = "block";
		document.getElementById("operateDiv").style.display = "block";
		document.getElementById("list").style.display = "block";
		document.getElementById("footer").style.display = "block";
	}
	

	
	

	var isChange = false;
	
	function changeFunc(){
		isChange = true;
		document.getElementById("msg").innerHTML = "";
	}
	
	function leaveBefore(){
		if( isChange == true ){
			return false;
		}
	}


	function replaceDoctorInfo(){
		var doctorListObject = document.getElementById("doctor");
		var doctorFirstName;
		var doctorSurname;
		var doctorTitle;
		var doctorAddress;
		for( var i = 0; i < doctorListObject.options.length; i++ ){
			if( doctorListObject.options[i].selected == true ){
				doctorFirstName = doctorListObject.options[i].getAttribute("firstName");
				doctorSurname = doctorListObject.options[i].getAttribute("surname");
				
				doctorTitle = doctorListObject.options[i].getAttribute("title");
				doctorAddress = doctorListObject.options[i].getAttribute("address");
				
			}
		}

		document.getElementById("addressDoctorName").innerHTML = doctorFirstName + "  " + doctorSurname;
		document.getElementById("letterAddress").innerHTML = doctorAddress;
		document.getElementById("letterDoctorName").innerHTML = doctorTitle + "  " + doctorFirstName;
		
	}


	jQuery(document).ready(function(){
		replaceDoctorInfo();
	});


	function doctorOnChange(){
		replaceDoctorInfo();
	}


	function replaceLetterHeader(){
		document.getElementById("replaceLandline").innerHTML = document.getElementById("landline").value;
		document.getElementById("replaceFax").innerHTML = document.getElementById("fax").value;
		document.getElementById("replaceAddress").innerHTML = document.getElementById("fromAddress").value;
		document.getElementById("replaceEmail").innerHTML = document.getElementById("email").value;


	}

	function previousPage(){
		var lastURL = document.referrer;

		if( lastURL.indexOf("patient_account.php") != -1 ){
			window.location.href = lastURL;
		}else{
			window.location.href = "index.php";
		}
	}


</script>
</head>

<body onbeforeunload="return leavebefore()">
	<div id="container">
		<div id="header"></div>
		<?php include_once 'nav.php';?>
		<?php 
			$ini_array = parse_ini_file("config/config.ini");
	
			$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
			mysqli_set_charset($conn, "utf8");
			ini_set('default_charset', "utf-8");
			$doctorsql = "select doctor_no, doctor_first_name, doctor_surname, doctor_address, doctor_title from doctor";
			$doctorresult = mysqli_query($conn, $doctorsql);
			$doctorno = 0;
			
			$patientno = 0;
			$patientfirstname = "";
			$patientsurname = "";
			$haspatientflag = false;
			$isfirstvisit = 0;
			if( isset($_get["patientno"]) && preg_match("/^\d+$/", $_get["patientno"]) == true){
				$patientno = mysqli_real_escape_string($conn, $_get["patientno"]);
				$patientsql = "select patient_no, first_name, surname, isfirstvisit from patient where patient_no=".$patientno;
				$patientresult = mysqli_query($conn, $patientsql);
				if( $patientrow = mysqli_fetch_array($patientresult) ){
					$patientfirstname = $patientrow["first_name"];
					$patientsurname = $patientrow["surname"];
					$isfirstvisit = $patientrow["isfirstvisit"];
					$haspatientflag = true;
				}				
			}
			
		?>
		<div id="main_content">
			<div id="list" style="margin:10px auto;text-align:center">
				<label>doctor list:</label> 
				<select  name="doctor"  id="doctor" onchange="doctoronchange()">
					<?php 
						while( $row = mysqli_fetch_array($doctorresult)){
							$doctornoinclinic = $row["doctor_no"];
							if( $doctorno == 0 ){
								$doctorno = $doctornoinclinic;
							}
							//$doctorname = $row["doctor_first_name"]." ".$row["doctor_surname"];
							$doctorFirstName = $row["doctor_first_name"];
							$doctorSurname = $row["doctor_surname"];
							$doctorAddress = $row["doctor_address"];
							$doctorTitle = $row["doctor_title"];
					?>
					<option  value="<?php echo $doctorNoInClinic?>" firstName="<?php echo $doctorFirstName?>" surname="<?php echo $doctorSurname?>" title="<?php echo $doctorTitle?>" address="<?php echo $doctorAddress?>" 
					<?php if( $doctorNo == $doctorNoInClinic){echo " selected=\"selected\"";}?>>
					<?php echo $doctorFirstName."  ".$doctorSurname?>
					</option>
					<?php }?>
				</select>
			
			</div> 
			<?php 
				date_default_timezone_set($ini_array["default_timezone"]);
				
				$fp = fopen("config/content.letter", "rw");
				$content = fread($fp, filesize("config/content.letter"));
				
				
			?>
			
			
			<div id="letterExample" style="text-align:center;border:2px solid black;height:650px;width:800px;margin:10px auto;">
				<div id="letterHeader" style="margin:10px auto;width:800px;">
					<table style="width:800px;">
						<tr>
							<td style="width:50%">
								<div id="upleftDiv">
									<div style="text-align:left;margin:0px 25px;"><label style="font-weight:bold;">Dr. Geraldine Mbrrow</label></div>
									<div style="text-align:left;margin:0px 25px;"><label> M.B., B.Ch., B.A.O., M.R.C.P.I.</label></div>
									<div style="text-align:left;margin:0px 25px;"><label style="font-weight:bold;">Consultant  Dermatologist.</label></div>
								</div>
							</td>
							<td style="text-align:center;width:50%;">
								<div><input type="text" name="landline" id="landline" value="<?php echo $ini_array["landline"];?>"/></div>
								<div><input type="text" name="fax" id="fax" value="<?php echo $ini_array["fax"]; ?>" /></div>
							</td>
						</tr>
						<tr>
							<td style="width:50%;text-align:left;"><div style="margin:0px 25px;"><input type="text" name="fromAddress" id="fromAddress" value="<?php echo $ini_array["fromAddress"]; ?>" size="38"/></div></td>
							<td style="width:50%"><input type="text" name="email" id="email" value="<?php echo $ini_array["email"];?>" size="38"/></td>
						</tr> 
					</table>
				</div>
				<div id="letterHeaderPrint" style="margin:10px auto;width:800px;display:none;">
					<table style="width:800px;">
						<tr>
							<td style="width:50%">
								<div id="upleftDiv">
									<div style="text-align:left;margin:0px 25px;"><label style="font-weight:bold;">Dr. Geraldine Mbrrow</label></div>
									<div style="text-align:left;margin:0px 25px;"><label> M.B., B.Ch., B.A.O., M.R.C.P.I.</label></div>
									<div style="text-align:left;margin:0px 25px;"><label style="font-weight:bold;">Consultant  Dermatologist.</label></div>
								</div>
							</td>
							<td style="text-align:center;width:50%;">
								<div><label id="replaceLandline"></label></div>
								<div><label id="replaceFax"></label></div>
							</td>
						</tr>
						<tr>
							<td style="width:50%;text-align:left;"><div style="margin:0px 25px;"><label id="replaceAddress"></label></div></td>
							<td style="width:50%"><label id="replaceEmail"></label></td>
						</tr> 
					</table>
				</div>
				<div id="letterMiddle" style="margin:0px;border:1px solid black;width:800px">
					<table style="width:800px;">
						<tr>
							<td style="width:5%;text-align:right;"><label style="font-weight:bold;">To:</label></td>
							<td style="width:50%;text-align:left;"><label id="addressDoctorName">Dr. Aisling O'Carroll,</label></td>
							<td style="text-align:right;width:15%"><label><?php if($hasPatientFlag == true ){?>Ref No.:<?php }?></label></td>
							<td style="width:30%;text-align:left;"><label><?php if( $hasPatientFlag == true ){echo $patientNo;}?></label></td>
						</tr>
						<tr>
							<td></td>
							<td style="width:65%;text-align:left;"><p id="letterAddress"></p></td>
							<td></td>
							<td style="text-align:left;"><?php if( $hasPatientFlag == true ){echo $patientFirstName." ".$patientSurname;}?></td>
						</tr>
						<tr>
							<td style="width:5%;text-align:right;"><label style="font-weight:bold;">Re:</label></td>
							<td style="width:65%;text-align:left;">
								<label>
								<?php 
									if( $hasPatientFlag == true ){
											if( $isFirstVisit == 1 ){
												echo "Thank you for referring ".$patientFirstName."  ".$patientSurname;
											}else{
												echo "I reviewed ".$patientFirstName."  ".$patientSurname;
											}
									}else{
										
									}
								?>
								</label>
							</td>
							<td colspan="2" style="width:30%;text-algin:center;"><?php echo date("d/m/Y")?></td>
						</tr>
						
					</table>
				</div>
				
				
				
				


				<div style="text-align:left;margin:10px;"><label>Dear <span id="letterDoctorName">Dr. O'Carroll.:</span></label></div>
				<div style="height:400px;width:670px;position:relative;left:40px;overflow:hidden;" id="letterContetnDiv">
					<textarea name="letterContent" id="letterContent" onclick="configChangeFunc()" cols="80" rows="23" style="resize:none;height:380px;"><?php echo $content?></textarea>
				</div>
				
			</div>
			<div id="operateDiv" style="text-align:center">
				<input type="button" value="Letter to GP's/Clinic" onclick="printLetter()"/>
				<input type="button" value="return" onclick="previousPage()"/>
				
				<label id="msg"></label>
			</div>
		</div>
		<?php include_once 'footer.php';?>
	</div>
</body>
</html>
